//
//  WebViewController.m
//  进度条
//
//  Created by KL on 16/11/21.
//  Copyright © 2016年 KM. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController ()<UIWebViewDelegate,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *web;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *act;

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UISearchBar *sear = [UISearchBar new];
    self.navigationItem.titleView = sear;//添加搜索框。
    sear.delegate = self;
    sear.returnKeyType = UIReturnKeySearch;
    sear.keyboardType = UIKeyboardTypeURL;//使键盘更有利于网址输入。
    
    //self.title = @"百度";
    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    NSURLRequest *re = [NSURLRequest requestWithURL:url];
    self.web.delegate = self;
    [self.web loadRequest:re];
    [self.act startAnimating];
    //UIBarButtonItem *it = [UIBarButtonItem alloc];
    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithTitle:@"<" style:UIBarButtonItemStylePlain target:self action:@selector(onback:)];
    UIBarButtonItem *forward = [[UIBarButtonItem alloc]initWithTitle:@">" style:UIBarButtonItemStylePlain target:self action:@selector(onforward:)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    self.toolbarItems = @[back,forward,space];

    
    
}

- (void)onback:(id)sender
{
    if ([self.web canGoBack])
    {
        [self.web goBack];
    }
}

- (void)onforward:(id)sender
{
    if ([self.web canGoForward])
    {
        [self.web goForward];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
     self.navigationController.toolbarHidden = NO;
}

- (void)viewDidDisappear:(BOOL)animated
{
    self.navigationController.toolbarHidden = YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.act stopAnimating];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar  //当搜索键被按下的时候
{
    [searchBar resignFirstResponder];
    NSLog(@"%@",searchBar.text);
    NSString *str = searchBar.text;
    if (![str hasPrefix:@"http:"])
    {
        str = [@"http://" stringByAppendingString:str];
    }
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *re = [NSURLRequest requestWithURL:url];
    [self.web loadRequest:re];
    
}



- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.act stopAnimating];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end